package com.gzhryc.shared_device.oem.h5.mng.controllers.place;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceFreeMember;
import com.gzhryc.shared_device.oem.code.place.services.PlaceFreeMemberService;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlaceFreeMemberSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class PlaceFreeMemberController {

	@Route(value = {"/place/free_member/list.html"})
    public String list(HttpServletRequest request) {
        return "/place/free_member/list.html";
    }

    @Route(value = "/place/free_member/list.data")
    public ListResult<PlaceFreeMember> listData(HttpServletRequest request) {
        ListResult<PlaceFreeMember> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        PlaceFreeMemberSearch search = WebUtils.getParameter(request, PlaceFreeMemberSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(PlaceFreeMemberService.self().countJoinBySearch(search));
        result.getBody().setData(PlaceFreeMemberService.self().findJoinBySearch(search, pageNo, pageLimit));
        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }
    
    @Route(value = { "/place/free_member/add.html" })
    public String add(HttpServletRequest request) {
        return "/place/free_member/edit.html";
    }

    @Route(value = { "/place/free_member/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        PlaceFreeMember placeFreeMember = WebUtils.getParameter(request, PlaceFreeMember.class);
        if (placeFreeMember != null && placeFreeMember.getPlaceId() != null && StringTools.isNotBlank(placeFreeMember.getPhone())) {
        	try {
				if(PlaceFreeMemberService.self().add(placeFreeMember)) {
					result.setCode(DtoConstants.SUCCESS, null);
					return result;
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
        }
        return result;
    }
    
    @Route(value = {"/place/free_member/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        Long placeId = WebUtils.getLong(request, "placeId");
        String phone = WebUtils.getString(request, "phone");
        try {
            if (PlaceFreeMemberService.self().deleteByKeys(placeId,phone)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
